#!/bin/bash
PIDOF="pidof -x"
function CleanDualBuffMemory(){
    rm -f /dev/shm/$1
    rm -f /dev/shm/$1.log
    rm -f /dev/shm/sem.$1
    rm -f /dev/shm/sem.$1.log
}
function CleanMemory(){
    # nml buff
    ipcrm -a    
}

function ReloadSetting() {
    if [ ! -f ./configs/JAKA/usersettings.ini ];then
        cp ./configs/default/usersettings.ini ./configs/JAKA
    fi
}

terminate_and_check() {
    local process_name=$1
    local pid=$(pgrep -f $process_name)

    # 如果找不到进程，则直接返回
    if [ -z "$pid" ]; then
        echo "进程 $process_name 未运行."
        return
    fi

    # 尝试优雅地终止进程
    kill $pid

    # 等待一段时间并检查进程是否还在运行
    sleep 2
    if pgrep -f $process_name > /dev/null ; then
        echo "无法优雅地终止进程 $process_name, 正在尝试强制终止..."
        kill -9 $pid
    else
        echo "成功终止进程 $process_name."
    fi
}


function Cleanup() {
    echo "Shutting down and cleaning up JkZuc..."
    terminate_and_check "zuctaskmod"
    terminate_and_check "redis-server"
    terminate_and_check "profinet"
    terminate_and_check "ethernetip"
    # terminate_and_check "python"
    CleanMemory
}

ClearLock(){
    rm -f /tmp/jkzuc.lock
}

Cleanup
CleanMemory
ClearLock